/**
 * Created by Administrator on 2016/8/6.
 */
// 百度统计代码
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?50440d5cb4232f7fe2e5468ffde69ad6";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();

// checkbox全选
function checkbox_all(){
    var box = document.getElementById('table').getElementsByTagName("input");
    for(var i=0;i<box.length;i++)
    {
        if(!box[i].checked)
        {
            box[i].checked=true;
        }
    }
}

// 反选
function checkbox_fan(){
    var box = document.getElementById('table').getElementsByTagName("input");
    for(var i=0;i<box.length;i++)
    {
        if(!box[i].checked)
        {
            box[i].checked=true;
        }else{
            box[i].checked=false;
        }
    }
}
// 不选
function checkbox_no(){
    var box = document.getElementById('table').getElementsByTagName("input");
    for(var i=0;i<box.length;i++)
    {
       box[i].checked=false;
    }
}

// 点击input  全选 与 反选
$("#input-all").on("click", function(event) {
    var all = document.getElementById('input-all');
    var box = document.getElementById('table').getElementsByTagName("input");
    all.onchange = function(){
        var chk = this.checked;
        for(var i = 0;i<box.length;i++){
            box[i].checked = chk;
        }
    }
});


// 批量删除
$('#del-all').click(function(){
    var text="";
    var len = $("input[name=ids]:checked").length;
    $("input[name=ids]:checked").each(function(index) {
        if(index == parseInt(len-1)){
            text += $(this).val();
        }else{
            text += $(this).val()+",";
        }
    });
    if(text == ''){
        layer.msg('请至少选择一项！');
        return false;
    }
    var url = $(this).attr('method');
    $.ajax({
        url:url,
        type:'post',
        dataType:'json',
        data:{ids:text},
        success:function(data){
            if(data.status == 200){
                layer.msg(data.msg, {icon: 1});
                $.each(data.ids,function(i,items){
                    $('.tr_'+items).remove();
                });
            }else{
                layer.msg(data.msg, {icon: 2});
            }
        }
    });
});

// 彻底删除
$('#Trash-all').click(function(){
    // 是否彻底删除的提示
    layer.confirm('彻底删除将无法恢复？', {icon: 3, title:'提示'}, function(index){
        var text="";
        var len = $("input[name=ids]:checked").length;
        $("input[name=ids]:checked").each(function(index) {
            if(index == parseInt(len-1)){
                text += $(this).val();
            }else{
                text += $(this).val()+",";
            }
        });
        if(text == ''){
            layer.msg('请至少选择一项！');
            return false;
        }
        var url = $(this).attr('method');
        $.ajax({
            url:url,
            type:'post',
            dataType:'json',
            data:{ids:text},
            success:function(data){
                if(data.status == 200){
                    layer.msg(data.msg, {icon: 1});
                    $.each(data.ids,function(i,items){
                        $('.tr_'+items).remove();
                    });
                }else{
                    layer.msg(data.msg, {icon: 2});
                }
            }
        });
    });
});

// 搜索js
$('.index_soso').click(function(){
    var keyword = $('.index_keyword').val();
    if(keyword == ''){
        layer.msg('请输入要搜索的内容！');
        return false;
    }
    $('.index_form').submit();

});

$('.index_sosos').click(function(){
    var keyword = $('.index_keywords').val();
    if(keyword == ''){
        layer.msg('请输入要搜索的内容！');
        return false;
    }
    $('.index_forms').submit();

});
// json 转化
$('.sub-json').click(function () {
    var json = $('.tools-json').val();
    if(json == ''){
        layer.msg('你又淘气了，啥都没有，你让我干啥！');
        return false;
    }
    //$('.json-form').submit();
    $.ajax({
        type: "POST",
        url:'/index.php/Tools/json.html',
        data:{content:json},
        dataType:'html',
        success: function(data) {
            $('.tools-json').val(data);
        }
    });
});

// html 有保留压缩
$('.sub-html').click(function () {
    var html = $('.tools-html').val();
    if(html == ''){
        layer.msg('你又淘气了，啥都没有，你让我干啥！');
        return false;
    }
    //$('.html-form').submit();
    $.ajax({
        type: "POST",
        url:'/index.php/Tools/html.html',
        data:{content:html,action:'yes'},
        dataType:'html',
        success: function(data) {
            $('.tools-html').val(data);
        }
    });
});

// html 无保留压缩
$('.sub-html-no').click(function () {
    var html = $('.tools-html').val();
    if(html == ''){
        layer.msg('你又淘气了，啥都没有，你让我干啥！');
        return false;
    }
    //$('.html-form').submit();
    $.ajax({
        type: "POST",
        url:'/index.php/Tools/html.html',
        data:{content:html,action:'no'},
        dataType:'html',
        success: function(data) {
            $('.tools-html').val(data);
        }
    });
});

// css 压缩
$('.sub-css').click(function () {
    var html = $('.tools-css').val();
    if(html == ''){
        layer.msg('你又淘气了，啥都没有，你让我干啥！');
        return false;
    }
    $.ajax({
        type: "POST",
        url:'/index.php/Tools/css.html',
        data:{content:html},
        dataType:'html',
        success: function(data) {
            $('.tools-css').val(data);
        }
    });
});

// base64 加密
$('.sub-base64-en').click(function(){
    var content = $('.tools-base64-en').val();
    if(content == ''){
        layer.msg('你又淘气了，啥都没有，你让我干啥！');
        return false;
    }
    $.ajax({
        type: "POST",
        url:'/index.php/Tools/base64.html',
        data:{content:content,action:'en'},
        dataType:'html',
        success: function(data) {
            $('.tools-base64-de').val(data);
        }
    });
});

// base解密
$('.sub-base64-de').click(function(){
    var content = $('.tools-base64-de').val();
    if(content == ''){
        layer.msg('你又淘气了，啥都没有，你让我干啥！');
        return false;
    }
    $.ajax({
        type: "POST",
        url:'/index.php/Tools/base64.html',
        data:{content:content,action:'de'},
        dataType:'html',
        success: function(data) {
            $('.tools-base64-en').val(data);
        }
    });
});

// sha 二进制加密
$('.sub-sha-2').click(function(){
    var content = $('.tools-sha-en').val();
    if(content == ''){
        layer.msg('你又淘气了，啥都没有，你让我干啥！');
        return false;
    }
    $.ajax({
        type: "POST",
        url:'/index.php/Tools/sha.html',
        data:{content:content,action:'2'},
        dataType:'html',
        success: function(data) {
            $('.tools-sha-de').val(data);
        }
    });
});

// sha 十六进制
$('.sub-sha-16').click(function(){
    var content = $('.tools-sha-en').val();
    if(content == ''){
        layer.msg('你又淘气了，啥都没有，你让我干啥！');
        return false;
    }
    $.ajax({
        type: "POST",
        url:'/index.php/Tools/sha.html',
        data:{content:content,action:'16'},
        dataType:'html',
        success: function(data) {
            $('.tools-sha-de').val(data);
        }
    });
});

// md5 16位加密
$('.sub-md5-16').click(function(){
    var content = $('.tools-md5-en').val();
    if(content == ''){
        layer.msg('你又淘气了，啥都没有，你让我干啥！');
        return false;
    }
    $.ajax({
        type: "POST",
        url:'/index.php/Tools/md5.html',
        data:{content:content,action:'16'},
        dataType:'html',
        success: function(data) {
            $('.tools-md5-de').val(data);
        }
    });
});

// md5 32位加密
$('.sub-md5-32').click(function(){
    var content = $('.tools-md5-en').val();
    if(content == ''){
        layer.msg('你又淘气了，啥都没有，你让我干啥！');
        return false;
    }
    $.ajax({
        type: "POST",
        url:'/index.php/Tools/md5.html',
        data:{content:content,action:'32'},
        dataType:'html',
        success: function(data) {
            $('.tools-md5-de').val(data);
        }
    });
});

// urlencode 加密
$('.sub-url-en').click(function(){
    var content = $('.tools-url-en').val();
    if(content == ''){
        layer.msg('你又淘气了，啥都没有，你让我干啥！');
        return false;
    }
    $.ajax({
        type: "POST",
        url:'/index.php/Tools/url.html',
        data:{content:content,action:'en'},
        dataType:'html',
        success: function(data) {
            $('.tools-url-de').val(data);
        }
    });
});

// urldecode 解密
$('.sub-url-de').click(function(){
    var content = $('.tools-url-de').val();
    if(content == ''){
        layer.msg('你又淘气了，啥都没有，你让我干啥！');
        return false;
    }
    $.ajax({
        type: "POST",
        url:'/index.php/Tools/url.html',
        data:{content:content,action:'de'},
        dataType:'html',
        success: function(data) {
            $('.tools-url-en').val(data);
        }
    });
});


// 清空表单里的数据
$('.sub-clear').click(function () {
    $('.tools-json').val('');
    $('.tools-html').val('');
    $('.tools-css').val('');
    $('.tools-base64-en').val('');
    $('.tools-base64-de').val('');
    $('.tools-sha-en').val('');
    $('.tools-sha-de').val('');
    $('.tools-md5-en').val('');
    $('.tools-md5-de').val('');
    $('.tools-url-en').val('');
    $('.tools-url-de').val('');
});

// 表单验证 -- 会员登录
$(function() {
    $('#LoginIn').validate({
        rules: {
            username: {
                required: true,
                minlength: 5
            },
            password: {
                required: true,
            }
        },
        messages: {
            username: {
                required: "用户名必须填写！",
                minlength: "用户名不能少于5个字符！"
            },
            password: {
                required: "密码不能为空！",
            }
        },
        errorPlacement: function(error, element) {
            error.appendTo(element.parent().parent());
        },
        errorElement: 'span',
        submitHandler: function (form) {
            $.ajax({
                type: 'post',
                url: $('#LoginIn').attr('action'),
                data: $('#LoginIn').serialize(),
                dataType: 'json',
                success: function (data) {
                    console.log(data);
                    if (data.status == 200) {
                        layer.msg(data.msg, {icon: 1, time: 2000}, function () {
                            location.reload();
                        });
                    } else {
                        layer.msg(data.msg, {icon: 2});
                    }
                }
            });
        }
    });
});
// 表单验证 -- 注册会员
$(function() {
    $('#Login_regist').validate({
        rules: {
            user: {
                required: true,
                minlength: 5
            },
            pwd: {
                required: true,
            },
            email:{
                required:true,
                email: true
            }
        },
        messages: {
            user: {
                required: "用户名必须填写！",
                minlength: "用户名不能少于5个字符！"
            },
            pwd: {
                required: "密码不能为空！",
            },
            email:{
                email: "请输入一个正确的邮箱",
            }
        },
        errorPlacement: function(error, element) {
            error.appendTo(element.parent().parent());
        },
        errorElement: 'span',
        submitHandler: function (form) {
            $.ajax({
                type: 'post',
                url: $('#Login_regist').attr('action'),
                data: $('#Login_regist').serialize(),
                dataType: 'json',
                success: function (data) {
                    if (data.status == 200) {
                        layer.msg(data.msg, {icon: 1, time: 2000}, function () {
                            location.reload();
                        });
                    } else {
                        layer.msg(data.msg, {icon: 2});
                    }
                }
            });
        }
    });
});

// 点击弹出登录框
$('#btn_login').click(function(){
    $('#mylogin').modal('show');
});
$('#btn_regist').click(function(){
    $('#mylogin').modal('show');
});

// 文章内页的代码高亮
$(window).load(function(){
    //代码高亮
	$("pre").addClass("prettyprint linenums");
    $(".content img").addClass("img-responsive "); // center-block
    prettyPrint();
});







